<?php
session_start();

// 检查是否已安装
$install_file = '../data/installed.txt';
if (!file_exists($install_file)) {
    header('Location: ../install/');
    exit;
}

// 检查管理员登录状态
if (!isset($_SESSION['admin_logged_in']) || !$_SESSION['admin_logged_in']) {
    header('Location: login.php');
    exit;
}

// 加载配置
if (!file_exists('../data/config.php')) {
    die('配置文件不存在');
}
require_once '../data/config.php';

// 数据库连接
try {
    $pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("数据库连接失败: " . $e->getMessage());
}

// 获取用户IP参数
$user_ip = $_GET['ip'] ?? '';
if (empty($user_ip)) {
    header('Location: index.php');
    exit;
}

// 获取该用户的聊天记录
$stmt = $pdo->prepare("SELECT message, response, timestamp FROM chat_history WHERE user_ip = ? ORDER BY timestamp ASC");
$stmt->execute([$user_ip]);
$chat_history = $stmt->fetchAll(PDO::FETCH_ASSOC);

// 获取用户信息
$stmt = $pdo->prepare("SELECT created_at FROM users WHERE ip_address = ?");
$stmt->execute([$user_ip]);
$user_info = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$user_info) {
    header('Location: index.php');
    exit;
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户聊天记录 - lanxAI管理后台</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f7fa;
            color: #333;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 15px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .header h1 {
            font-size: 1.5em;
            font-weight: 600;
        }
        
        .back-btn {
            background: rgba(255, 255, 255, 0.2);
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 5px;
            cursor: pointer;
        }
        
        .back-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }
        
        .container {
            max-width: 1200px;
            margin: 20px auto;
            padding: 0 20px;
        }
        
        .user-info {
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
            margin-bottom: 20px;
        }
        
        .chat-container {
            background: white;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
            overflow: hidden;
        }
        
        .chat-header {
            padding: 15px 20px;
            background: #f8f9fa;
            border-bottom: 1px solid #eee;
            font-weight: 600;
        }
        
        .chat-history {
            height: 500px;
            overflow-y: auto;
            padding: 20px;
            background: #f9f9f9;
        }
        
        .message {
            margin-bottom: 15px;
            padding: 10px 15px;
            border-radius: 10px;
            max-width: 80%;
            word-wrap: break-word;
            line-height: 1.5;
        }
        
        .user-message {
            background: #667eea;
            color: white;
            margin-left: auto;
            text-align: right;
        }
        
        .ai-message {
            background: #e9ecef;
            color: #333;
            margin-right: auto;
        }
        
        .timestamp {
            font-size: 0.7em;
            opacity: 0.7;
            margin-top: 5px;
            display: block;
        }
        
        .footer {
            text-align: center;
            padding: 20px;
            color: #666;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>用户聊天记录</h1>
        <a href="index.php" class="back-btn">返回管理后台</a>
    </div>
    
    <div class="container">
        <div class="user-info">
            <h2>用户信息</h2>
            <p><strong>IP地址:</strong> <?php echo htmlspecialchars($user_ip); ?></p>
            <p><strong>注册时间:</strong> <?php echo htmlspecialchars($user_info['created_at']); ?></p>
        </div>
        
        <div class="chat-container">
            <div class="chat-header">聊天记录</div>
            <div class="chat-history" id="chatHistory">
                <?php if (count($chat_history) > 0): ?>
                    <?php foreach ($chat_history as $chat): ?>
                        <div class="message user-message">
                            <strong>用户:</strong> <?php echo nl2br(htmlspecialchars($chat['message'])); ?>
                            <span class="timestamp"><?php echo htmlspecialchars($chat['timestamp']); ?></span>
                        </div>
                        <div class="message ai-message">
                            <strong>AI:</strong> <?php echo nl2br(htmlspecialchars($chat['response'])); ?>
                            <span class="timestamp"><?php echo htmlspecialchars($chat['timestamp']); ?></span>
                        </div>
                    <?php endforeach; ?>
                <?php else: ?>
                    <p style="text-align: center; color: #666; padding: 20px;">该用户暂无聊天记录</p>
                <?php endif; ?>
            </div>
        </div>
    </div>
    
    <div class="footer">
        <p>LANX开源AI</p>
    </div>

    <script>
        // 自动滚动到最新消息
        const chatHistory = document.getElementById('chatHistory');
        chatHistory.scrollTop = chatHistory.scrollHeight;
    </script>
</body>
</html>